home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4698 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: variable length string
  5. Date: 6 Feb 1996 15:35:01 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4f7sf5$emk@sparcserver.lrz-muenchen.de>
  9. References: <4f5s97$8sho@tigger.cc.uic.edu> <9602052314.AA18664@dxmint.cern.ch> <4f6ma1$613e@tigger.cc.uic.edu>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. Qi Zeng <zeng@sunphy1.phy.uic.edu> writes:
  13.  
  14. >Dan Pop <danpop@mail.cern.ch> wrote:
  15. >>
  16. >> Qi Zeng <zeng@sunphy1.phy.uic.edu> writes:
  17. >> >  >Suppose you want to read from stdin a string, but you do not know how 
  18. >> >long it will be. You have to assign the string to some char*,  
  19. >> 
  20. >> Use fgets with a reasonably sized buffer.  After reading the string,
  21. >> use strlen to find out its real length, malloc the right amount of memory 
  22. >> and copy the string there (dropping the terminating '\n').
  23. >> > Dan
  24. >Thanks.
  25.  
  26. >And yes, it is a way to do that. But how to deal with the space you 
  27. >malloced? Say you return a char *, which points to the space you jsut 
  28. >malloced. When do you free them? 
  29.  
  30. Aehm, when you don't need it any more, maybe? This question should
  31. definitely be in the "other" FAQ.
  32.  
  33. Managing your dynamically allocated memory is a burden that you have
  34. to bear to avoid the need for "garbage collection" or something 
  35. similar.
  36.  
  37. Kurt
  38. --
  39. | Kurt Watzka                             Phone : +49-89-2180-6254
  40. | watzka@stat.uni-muenchen.de
  41. | ua302aa@sunmail.lrz-muenchen.de
  42.